package com.iotek.ssm.controller;

import com.iotek.ssm.pojo.Attendance;
import com.iotek.ssm.pojo.AttendanceQueryVo;
import com.iotek.ssm.pojo.Resume;
import com.iotek.ssm.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    //查询获取考勤表
    @RequestMapping("/findAttendances")
    @ResponseBody
    public List<Attendance> findAttendances(AttendanceQueryVo attendanceQueryVo) {
        List<Attendance> attendances = null;
        try {
            attendances = attendanceService.findAttendanceList(attendanceQueryVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("**************" + attendances);
        return attendances;
    }

    //查询获取出勤天数
    @RequestMapping("/getOnJobDays")
    @ResponseBody
    public Integer getOnJobDays(AttendanceQueryVo attendanceQueryVo) {
        try {
            Integer days = attendanceService.getCounts(attendanceQueryVo);
            return days;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //上班打卡，判断有无迟到
    @RequestMapping("/userSignIn")
    @ResponseBody
    public Integer userSignIn(Attendance attendance, @RequestParam("times") Integer times) {
//        attendance.setGoTime(new Date());

        if (times > 0)   //设置迟到时长，单位：分钟
            attendance.setBeLateTimes(times);
        if (times < 0)
            attendance.setBeLateTimes(0);
        try {
            attendanceService.addAttendance(attendance);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //下班打卡，判断有无早退以及加班
    @RequestMapping("/userSignOut")
    @ResponseBody
    public Integer userSignOut(Attendance attendance, @RequestParam("times") Integer times) {
//        attendance.setOffTime(new Date());

        if (times < 0)    //设置早退时长，单位：分钟
            attendance.setLeaveEarlyTimes(times);

        System.out.println("times = " + times + "---times/60 = " + (times / 60) + "---times%60 = " + (times % 60));

        if (times > 0) {   //计算并设置加班时长，单位：小时，步进：0.5
            float extraTimes = 0;
            if (times / 60 > 0) {    //加班由1小时开始计算
                if (times % 60 > 0 && times % 60 < 30)
                    extraTimes = times / 60;
                else
                    extraTimes = (float) (times / 60 + 0.5);
            }
            attendance.setExtraWorkTimes(extraTimes);
        }

        try {
            attendanceService.updateAttendance(attendance);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
